#include "CalendarAdvance.h"
#include "Calendar.h"
#include <stdio.h>
#include <assert.h>
#include <iostream>
#include <conio.h>
using namespace std;

#define MAX_FIND_NOF_MEETINGS 100
#define MAX_SUBJECT_LENGTH  100
#define MAX_NAMES_LENGTH 100

void getMeetingParams(char *&subject, int &day, float &startTime, float &endTime, char *&names, int flags) {
	
	if (flags != 0) {
		subject = new char[MAX_SUBJECT_LENGTH];
	}
	
	if (flags == 2) {
		names = new char[MAX_NAMES_LENGTH];
	}
	_flushall(); 
	
	if (flags != 0) {
		cout << "Type meeting subject.\n >";
		cin.getline (subject, MAX_SUBJECT_LENGTH);
		_flushall();
	}
	cout << "Type meeting day (1-7).\n >";
	cin >> day;
	_flushall();
	cout << "Type meeting start time.\n >";
	cin >> startTime;
	_flushall();
	cout << "Type meeting end time.\n >";
	cin >> endTime;
	_flushall();

	if (flags == 2) {
		cout << "Type names separate by space.\n >";
		cin.getline (names, MAX_NAMES_LENGTH);
		_flushall();
	}
}

int main(int argc, const char* argv[]){

#if 1
	int choice, calChoice = 0;
	Calendar cal(7);
	CalendarAdvance calAdvance(7);
	CalendarError res;
	float startTime, endTime;
	int day;
	char *subject, *names;
	const Meeting *meeting, *newMeeting;
	bool quit = false;
	const Meeting **meetings;
	int max, nofMeetings;

	while (calChoice != 1 && calChoice != 2) {
		cout <<  "1. Calendar" 	<< endl ;            
		cout <<  "2. Calendar with names" 	<< endl ;           
		cout <<  "Enter your choice: " 	<< endl ;      
		cin >> calChoice ;
		_flushall();
	}


	while(!quit)  {
		cout <<  "1. Add appoiments." 	<< endl ;            
		cout <<  "2. Remove appoiment" 	<< endl ;           
		cout <<  "3. move appoiment"		<< endl ;
		cout <<  "4. Clean Diary. "		<< endl ;         
		cout <<  "5. Find appoiment "		<< endl ;       
		cout <<  "6. Print Diary "		<< endl ;
		cout <<  "7. Quit." 			<< endl ;      
		cout <<  "Enter your choice: " 	<< endl ;      
		cin >> choice ;
		_flushall();

		switch (choice)  {
		case 1:

			getMeetingParams(subject, day, startTime, endTime, names, calChoice);
			if (calChoice == 1) {
				res = cal.createMeeting(subject, startTime, endTime, day, meeting);
			} else {
				res = calAdvance.createMeeting(subject, startTime, endTime, day, names, meeting);
			}
			if (res != CALENDAR_ERROR_NONE) {
				cout << "Error." << endl;
				break;
			}

			if (calChoice == 1) {
				res = cal.addMeeting(*meeting);
			} else {
				res = calAdvance.addMeeting(*meeting);
			}
			if (res == CALENDAR_ERROR_OVERLAP) {
				cout << "ERROR: Meeting is overlap" << endl;
				delete subject;
				break;
			} else if (res != CALENDAR_ERROR_NONE) {
				cout << "Error." << endl;
				break;
			}
			
			break; 			
		case 2:

			getMeetingParams(subject, day, startTime, endTime, names, 1); /*need no names*/
			if (calChoice == 1) {
				res = cal.getMeeting(subject, startTime, endTime, day, meeting);
			} else {
				res = calAdvance.getMeeting(subject, startTime, endTime, day, meeting);
			}
			if (res == CALENDAR_ERROR_NOT_EXIST) {
				cout << "ERROR: Meeting not exist." << endl;
				break;
			} else if (res != CALENDAR_ERROR_NONE) {
				cout << "Error." << endl;
				break;
			}

			if (calChoice == 1) {
				res = cal.deleteMeeting(*meeting);
			} else {
				res = calAdvance.deleteMeeting(*meeting);
			}
			if (res != CALENDAR_ERROR_NONE) {
				cout << "Error." << endl;
				break;
			}
			break;
		case 3:
			cout << "Old Meeting info" << endl;
			getMeetingParams(subject, day, startTime, endTime, names, 1); /*need no names*/
			if (calChoice == 1) {
				res = cal.getMeeting(subject, startTime, endTime, day, meeting);
			} else {
				res = calAdvance.getMeeting(subject, startTime, endTime, day, meeting);
			}
			if (res == CALENDAR_ERROR_NOT_EXIST) {
				cout << "ERROR: Meeting not exist." << endl;
				break;
			} else if (res != CALENDAR_ERROR_NONE) {
				cout << "Error." << endl;
				break;
			}

			cout << "Move to" << endl;
			getMeetingParams(subject, day, startTime, endTime, names, 0); /*need no names and subject*/
			if (calChoice == 1) {
				res = cal.moveMeeting(*meeting, startTime, endTime, day , newMeeting);
			} else {
				res = calAdvance.moveMeeting(*meeting, startTime, endTime, day , newMeeting);
			}
			if (res != CALENDAR_ERROR_NONE) {
				cout << "Error." << endl;
				break;
			}
			break;
		case 4:
			if (calChoice == 1) {
				cal.clean();
			} else {
				calAdvance.clean();
			}
			break;
		case 5:
			meetings = new const Meeting*[MAX_FIND_NOF_MEETINGS];
			max  = MAX_FIND_NOF_MEETINGS;

			cout << "Type start time." << endl;
			cin >> startTime;
			if (calChoice == 1) {
				res = cal.findMeetingStartingAt(startTime, meetings, max, nofMeetings);
			} else {
				res = calAdvance.findMeetingStartingAt(startTime, meetings, max, nofMeetings);
			}
			if (res != CALENDAR_ERROR_NONE) {
				cout << "Error." << endl;
				break;
			}

			for (int i=0; i<nofMeetings; i++) {
				meetings[i]->printAll();
			}

			delete meetings;
			break;
		case 6:
			if (calChoice == 1) {
				res = cal.printAll();
			} else {
				res = calAdvance.printAll();
			}
			if (res != CALENDAR_ERROR_NONE) {
				cout << "Error." << endl;
				break;
			}

			break;
		case 7:
			quit = true;
			break;
		}		
	}
#endif
}
